package com.lanou.elh.cart.web.controller;

import com.lanou.elh.cart.cartTool.CartTool;
import com.lanou.elh.cart.entity.Cart;
import com.lanou.elh.cart.entity.Item;
import com.lanou.elh.cart.service.CartService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @PostMapping("/add")
    @ApiOperation(value = "添加购物车")
    public Map<String,Object> add(@RequestBody Map<String,Object> cart){
        cartService.add(cart);
        CartTool cartTool = new CartTool();
        Map<String,Object> map = cartTool.addCart();
        return map;
    }

    @GetMapping("/list")
    @ApiOperation(value = "显示购物车")
    public Map<String,Object> list(Integer userId){
        System.out.println(userId);
        List<Item> list = cartService.list(userId);

        Map<String,Object> map = new HashMap<>();
        map.put("result",list);
        return map;
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除购物车")
    public Map<String,Object> del(@RequestBody Map<String,Object> cartOne){
        cartService.del(cartOne);
        Map<String,Object> map = new HashMap<>();
        return map;
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改购物车商品数量")
    public Map<String,Object> edit(@RequestBody Map<String,Object> editCart){
        cartService.editCart(editCart);
        Map<String,Object> map = new HashMap<>();
        return map;
    }

    @RequestMapping("/show")
    public String show(){
        cartService.show();
        return "success show";
    }

    @RequestMapping("/set")
    public void set() {
        cartService.set();
    }
}
